#include "mycommandgenerator.h"
#include <vector>
#include <QDebug>

#include "serial_sample_data.h"

MyCommandGenerator::MyCommandGenerator()
{

}

int MyCommandGenerator::hello(MyCommand &cmd)
{
    cmd.ack = SERIAL_ACK_REQUIRED;
    cmd.cmd = CMD_HELLO;
    cmd.mode = SERIAL_MODE_SYNCHRONOUS;

    cmd.data.append((char)0xEF);
    cmd.data.append((char)0xAA);
    cmd.data.append((char)0x11);
    cmd.data.append((char)0x00);
    cmd.data.append((char)0x00);
    cmd.data.append((char)0x11);

    return cmd.data.length();
}

int MyCommandGenerator::identifyUser(MyCommand &cmd)
{
    cmd.ack = SERIAL_ACK_REQUIRED;
    cmd.cmd = CMD_IDENTIFY_USER;
    cmd.mode = SERIAL_MODE_SYNCHRONOUS;

    std::vector<char> vec{(char)0xEF, (char)0xAA, (char)0x12, (char)0x00, (char)0x02, (char)0x00, (char)0x00, (char)0x10, '\r', '\n'};
    QByteArray data(vec.data(), vec.size());
    cmd.data = data;

    return cmd.data.length();
}


static int data_index = 0;


int MyCommandGenerator::canfdToggle()
{
    data_index++;
    return data_index;
}

int MyCommandGenerator::peakdata(MyCommand &cmd, int size, int gbytefactor)
{
    cmd.ack = SERIAL_NO_ACK;
    cmd.mode = SERIAL_MODE_ASYNCHRONOUS;

    static int index = 0;
    if (cmd.timeout == -1) {
        index = 0;
        return 0;
    }

    if (index % 8 == 0) qDebug() << "index" << data_index << "-" << index;
    QByteArray data;

    if (gbytefactor == 2)
        if (data_index % 2 == 0)
            data = QByteArray((char*)peakdata_gray32_1[index], sizeof(peakdata_gray32_1[index]));
        else
            data = QByteArray((char*)peakdata_gray32_2[index], sizeof(peakdata_gray32_2[index]));
    else
        if (data_index % 2 == 0)
            data = QByteArray((char*)peakdata5[index], sizeof(peakdata5[index]));
        else
            data = QByteArray((char*)peakdata6[index], sizeof(peakdata6[index]));

    if (index % (size-1) == 0) qDebug() << "data" << data.length() << data.left(64).toHex(' ');
    cmd.data = data;
    index = (index +1) % size;

    return cmd.data.length();
}

int MyCommandGenerator::pointdata(MyCommand &cmd,int size, int)
{
    cmd.ack = SERIAL_NO_ACK;
    cmd.mode = SERIAL_MODE_ASYNCHRONOUS;

    static int index = 0;
    if (cmd.timeout == -1) {
        index = 0;
        return 0;
    }

    if (index % 9 == 0) qDebug() << "index" << data_index << "-" << index;
    QByteArray data;
    if (data_index % 2 == 0)
        data = QByteArray((char*)pointdata1[index], sizeof(pointdata1[index]));
    else
        data = QByteArray((char*)pointdata2[index], sizeof(pointdata2[index]));

    if (index % (size-1) == 0) qDebug() << "data" << index << data.length() << data.left(16).toHex(' ');
    cmd.data = data;
    index = (index +1) % size;

    return cmd.data.length();
}


int MyCommandGenerator::debugdata(MyCommand &cmd, int size, int)
{
    cmd.ack = SERIAL_NO_ACK;
    cmd.mode = SERIAL_MODE_ASYNCHRONOUS;

    static int index = 0;
    if (cmd.timeout == -1) {
        index = 0;
        return 0;
    }

    if (index % 8 == 0) qDebug() << "index" << data_index << "-" << index;
    QByteArray data;
    if (data_index % 2 == 0)
        data = QByteArray((char*)debugdata3[index], sizeof(debugdata3[index]));
    else
        data = QByteArray((char*)debugdata4[index], sizeof(debugdata4[index]));

    if (index % (size-1) == 0) qDebug() << "data" << data.length() << data.left(64).toHex(' ');
    cmd.data = data;
    index = (index +1) % size;

    return cmd.data.length();
}
